<?php
    session_start();
    include "../../function/functionPHP.php";
    include "../../class/crop.class.php";
    include "../../class/rice.class.php";
    include "../../class/cost.class.php";
    include "../../class/farm.class.php";
    include "../../class/member.class.php";
    include "../../class/date.class.php";
    $mysqli=  connect();
    $cropID=$_POST["cropID"];
    $costID=$_POST["costID"];
    $costObj=new cost();
    $mainCost=$costObj->getMainCost();
    
    
    $color[]="#6dff7b";
    $color[]="#207eff";
    $color[]="#ffa9ec";
    $color[]="#B35B5B";
    $color[]="rgb(134, 255, 214)";
    $color[]="#148C3A";
    $color[]="#ECAD86";
    $color[]="#80B8C5";
    $color[]="A03939";
    
    $dateClass=new date();
    if(!is_array($cropID)){
        ?>
<div style="text-align: center;">กรุณาเลือก Crop</div>
            <?php
        exit();
    }else{
        foreach ($cropID as $keyCrop => $value) {
            $cropClass=new crop($value);
            $crop=$cropClass->getCrop();
            $farmID=$crop["farmID"];
            $farmClass=new farm($farmID);
            $farm=$farmClass->getFarm();
            $memID=$farm["memID"];
            $memberClass=new member($memID);
            $member[$keyCrop]=$memberClass->getMember();
            foreach ($mainCost as $keyCost => $valueM) {
                $val[$keyCrop][$keyCost]=$cropClass->percentCost($keyCost);
            }
        }
    }
?>
<center>
    แผนภูมิแท่งเปรียบเทียบต้นทุนการปลูกข้าว<br>
</center>
<br>
<table class="noSpacing"style="width: 100%;height: 100%;">
    <tr>
        <td style="vertical-align: middle">
            <div style="border: none;margin-left: auto;margin-right: auto;display: table;">
                <table class="noSpacing" style="margin-left: 30px;margin-right: 30px;margin-top: 20px;">
                    <tr>
                        <td style="vertical-align: top;border-right: 1px solid #000;">
                            <div style="position: relative;height: 100%;">
                                <div style="position: absolute;top: -25px;left:-18px;font-size: 60%;white-space: pre;">บาท/ไร่</div>
                            </div>
                        </td>
                        <?php
                            foreach ($val as $key => $value) {
                                $max[]=max($value);
                            }
                            $maxVal=max($max);
                            $maxHeight=250;
                            foreach ($mainCost as $keyCost => $valueCost) {
                                ?>
                        <td style="vertical-align: bottom;text-align: center;font-size: 80%;padding: 5px 10px 5px 10px;cursor: pointer;"
                            class="hover"
                            onclick="showSubGraph('<?=$keyCost?>')"
                            >
                            <div style="position: relative">
                                <table class="noSpacing">
                                    <tr>
                                        <?php
                                    $i=0;
                                    foreach ($cropID as $keyCrop => $valueCrop) {
                                        if($maxVal==0){
                                            $height=0;
                                        }else{
                                            $height=((100*$val[$keyCrop][$keyCost])/$maxVal)*($maxHeight/100);
                                        }
                                        $height=$val[$keyCrop][$keyCost]*6;
                                        ?>
                                        <td style="vertical-align: bottom;font-size: 60%;">
                                            <?=  number_format($val[$keyCrop][$keyCost])?>%
                                            <div 
                                                style="
                                                    background-color: <?=$color[$i]?>;
                                                    width: 25px;
                                                    height: <?=$height?>px;
                                                "></div>
                                        </td>
                                            <?php
                                        $i++;
                                    }
                                    ?>
                                    </tr>
                                </table>
                                <div style="
                                    position: absolute;
                                    display: none;
                                    width: 100%;
                                    height: <?=$valueCost["stdPercent"]*6?>px;
                                    bottom: 0px;
                                    border-top: 1px dashed #aaa;
                                "></div>
                            </div>
                        </td>
                                    <?php
                            }
                        ?>
                    </tr>
                    <tr>
                        <td></td>
                        <?php
                        foreach ($mainCost as $key => $value) {
                                ?>
                        <td style="vertical-align: top;text-align: center;font-size: 60%;padding: 5px;border-top: 1px solid #000;">
                            <div style="max-width: 70px;margin-left: auto;margin-right: auto;">
                                <?php
                                    $costObj = new cost($key);
                                    $costName=$costObj->getCost();
                                    echo $costName["costName"];
                                ?>
                            </div>
                        </td>
                                    <?php
                            }
                        ?>
                    </tr>
                </table>
            </div>
            <?php require dirname(__FILE__).'/subGraph.php';?>
            <div style="border: 1px solid #428bca;margin-top: 30px;margin-left: auto;margin-right: auto;display: table;padding: 0px;font-size: 80%;">
                <table class="noSpacing pc" style="">
                    <tr style="text-align: center;background-color: #428bca;color: #fff;font-size: 80%;">
                        <td style="padding: 5px 10px 5px 10px;"></td>
                        <td style="padding: 5px 10px 5px 10px;">ชื่อ นามสกุล / วันที่เริ่มปลูก</td>
                        <td style="width: 150px;padding: 5px 10px 5px 10px;text-align: right">ต้นทุน (บาท/ไร่)</td>
                        <td style="width: 150px;padding: 5px 10px 5px 10px;text-align: right">รายได้ (บาท/ไร่)</td>
                        <td style="width: 150px;padding: 5px 10px 5px 10px;text-align: right">กำไร (บาท/ไร่)</td>
                        <td style="width: 150px;padding: 5px 10px 5px 10px;text-align: right">กำไร (%)</td>
                    </tr>
                    <?php
                        $i=0;
                        foreach ($cropID as $keyCrop => $value) {
                            $cropClass=new crop($value);
                            $crop=$cropClass->getCrop();
                            
                            $riceObj=new rice($crop["rvID"]);
                            $rice=$riceObj->getRice();
                            
                            $sumCost=$cropClass->sumCost();
                            $sumIncome=$cropClass->sumIncome();
                            $profit=$cropClass->profit();
                            ?>
                    <tr>
                        <td style="padding: 5px;text-align: center;border-bottom: 1px dashed #aaa;">
                            <div style="width: 30px;height: 30px;background-color: <?=$color[$i]?>"></div>
                        </td>
                        <td style="padding: 5px 10px 5px 10px;border-bottom: 1px dashed #aaa;">
                            <?php
                                if((memAuthenticated() && $member["private"]!="1") || ofAuthenticated()){
                                    echo $member[$keyCrop]["title"]." ".$member[$keyCrop]["firstName"]." ".$member[$keyCrop]["lastName"];
                                }else{
                                    echo "เกษตรกร";
                                }
                            ?> (<?=$rice["rvName"]?>)
                            <div style="font-size: 80%;">
                                <?=$dateClass->dateEncodeBE($crop["startDate"])?> - <?=$dateClass->dateEncodeBE($crop["endDate"])?>
                            </div>
                        </td>
                        <td style="padding: 5px;text-align: right;border-bottom: 1px dashed #aaa;">
                            <?=  number_format($sumCost,2);?>
                        </td>
                        <td style="padding: 5px;text-align: right;border-bottom: 1px dashed #aaa;">
                            <?=  number_format($sumIncome,2);?>
                        </td>
                        <td style="padding: 5px;text-align: right;border-bottom: 1px dashed #aaa;">
                            <?=  number_format($profit,2);?>
                        </td>
                        <td style="padding: 5px;text-align: right;border-bottom: 1px dashed #aaa;font-weight: bold;">
                            <?php
                                if($sumCost==0){
                                    echo 0;
                                }else{
                                    echo number_format(($profit*100)/$sumCost,2);
                                }
                            ?>%
                        </td>
                    </tr>
                                <?php
                            $i++;
                        }
                    ?>
                </table>
                <table class="noSpacing mobile" style="">
                    <tr style="text-align: center;background-color: #428bca;color: #fff;font-size: 80%;">
                        <td style="padding: 5px 10px 5px 10px;"></td>
                        <td style="width: 150px;padding: 5px 10px 5px 10px;text-align: right">ต้นทุน (บาท/ไร่)</td>
                        <td style="width: 150px;padding: 5px 10px 5px 10px;text-align: right">รายได้ (บาท/ไร่)</td>
                        <td style="width: 150px;padding: 5px 10px 5px 10px;text-align: right">กำไร (บาท/ไร่)</td>
                        <td style="width: 150px;padding: 5px 10px 5px 10px;text-align: right">กำไร (%)</td>
                    </tr>
                    <?php
                        $i=0;
                        foreach ($cropID as $keyCrop => $value) {
                            $cropClass=new crop($value);
                            $crop=$cropClass->getCrop();
                            
                            $riceObj=new rice($crop["rvID"]);
                            $rice=$riceObj->getRice();
                            
                            $sumCost=$cropClass->sumCost();
                            $sumIncome=$cropClass->sumIncome();
                            $profit=$cropClass->profit();
                            ?>
                    <tr>
                        <td style="padding: 5px;text-align: center;border-bottom: 1px dashed #aaa;" rowspan="2">
                            <div style="width: 30px;height: 30px;background-color: <?=$color[$i]?>"></div>
                        </td>
                        <td style="padding: 5px 10px 5px 10px;" colspan="4">
                            <?=$member[$keyCrop]["title"]." ".$member[$keyCrop]["firstName"]." ".$member[$keyCrop]["lastName"];?>
                             (<?=$rice["rvName"]?>)
                            <div style="font-size: 80%;float: right;">
                                <?=$dateClass->dateEncodeBE($crop["startDate"])?> - <?=$dateClass->dateEncodeBE($crop["endDate"])?>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td style="padding: 5px;text-align: right;border-bottom: 1px dashed #aaa;">
                            <?=  number_format($sumCost,2);?>
                        </td>
                        <td style="padding: 5px;text-align: right;border-bottom: 1px dashed #aaa;">
                            <?=  number_format($sumIncome,2);?>
                        </td>
                        <td style="padding: 5px;text-align: right;border-bottom: 1px dashed #aaa;">
                            <?=  number_format($profit,2);?>
                        </td>
                        <td style="padding: 5px;text-align: right;border-bottom: 1px dashed #aaa;font-weight: bold;">
                            <?php
                                if($sumCost==0){
                                    echo 0;
                                }else{
                                    echo number_format(($profit*100)/$sumCost,2);
                                }
                            ?>%
                        </td>
                    </tr>
                                <?php
                            $i++;
                        }
                    ?>
                </table>
            </div>
        </td>
    </tr>
</table>
<script type="text/javascript">
    function showSubGraph(costID){
        $('.subGraphAll').slideUp();
        $('#subGraph').fadeIn();
        $('#subGraph'+costID).slideDown();
    }
</script>